﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PTUDW_CNTN2010_F_CDT.Models;
using System.Diagnostics;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    public class AdminProductController : Controller
    {
        EcommerceDBContext db = new EcommerceDBContext();

        public ActionResult ViewProduct()
        {
            return View();
        }

        public JsonResult GetListProducts(int? page, int? rows, string sidx, string sord)
        {
            var assets = from c in db.Products
                         orderby c.ProductName
                         select new
                         {
                             ID = c.ProductId,
                             ProductName = c.ProductName,
                             Price = c.Price,
                             Size = c.Size,
                             Weight = c.Weight,
                             Amount = c.Amount,
                             Warranty = c.Warranty,
                             Rating = c.Rating,
                             NumRating = c.NumRating
                         };

            //var sortedAssets = assets.SortBy(sidx, sord.Contains("DESC"));
            var pagedAssets = assets.ToPagedList(page ?? 1, rows ?? 20);

            var results = new
            {
                total = pagedAssets.PageCount, //number of pages
                page = pagedAssets.PageNumber, //current page
                records = assets.Count(), //total items
                rows = pagedAssets
            };

            return Json(results);
        }

        public void Delete(int ID)
        {
            var product = db.Products.Find(ID);
            db.Products.Remove(product);
            db.SaveChanges();
        }

        [HttpPost]
        public void SaveChange(int ID, string ProductName, float Price, string Size, string Weight, int Amount, int Warranty, float Rating, int NumRating)
        {
            var product = db.Products.Find(ID);
            if (product == null)
            {
                product = new Product();
                db.Products.Add(product);
                product.ImageUrl = null;
                product.Model3DUrl = null;
                product.CategoryId = 1;
                product.Description = "";
            }

            product.ProductName = ProductName;
            product.Price = Price;
            product.Size = Size;
            product.Weight = Weight;
            product.Amount = Amount;
            product.Warranty = Warranty;
            product.Rating = Rating;
            product.NumRating = NumRating;

            db.SaveChanges();
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
        }
    }
}
